﻿unit unit1;

interface

uses
     //
     dwBase,

     
     //
     CloneComponents,
     SynCommons,
     //ZAbstractRODataset, ZDataset, ZConnection,ZAbstractDataset, ZAbstractConnection,

     //
     Math,
     Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     Dialogs, StdCtrls, jpeg, ExtCtrls, DB,  Data.Win.ADODB, Vcl.Imaging.pngimage;

type
  TForm1 = class(TForm)
    Panel_All: TPanel;
    Panel_99_Foot: TPanel;
    Label14: TLabel;
    Label15: TLabel;
    Panel4: TPanel;
    Panel_00_Title: TPanel;
    Panel_00_New: TPanel;
    Label_Floor: TLabel;
    ADOQuery: TADOQuery;
    Panel_00_Banner: TPanel;
    Panel_00_Center: TPanel;
    Image_logo: TImage;
    Label_FAQs: TLabel;
    Label_Download: TLabel;
    Label_ContactUs: TLabel;
    Label_gitee: TLabel;
    Label_blog: TLabel;
    Label3: TLabel;
    Label10: TLabel;
    Forum1: TStaticText;
    Forum2: TStaticText;
    StaticText_Home: TStaticText;
    Forum5: TStaticText;
    Forum7: TStaticText;
    Forum4: TStaticText;
    Forum6: TStaticText;
    Forum3: TStaticText;
    Button_Login: TButton;
    Button_User: TButton;
    Button_Logout: TButton;
    Panel_01_Content: TPanel;
    Panel_01_Center: TPanel;
    Panel_00_Blank: TPanel;
    Panel_11_Reply: TPanel;
    Memo_New: TMemo;
    Panel_01_Rooms: TPanel;
    ComboBox_Rooms: TComboBox;
    Panel_02_Subject: TPanel;
    Edit_Subject: TEdit;
    Panel_12_Replay: TPanel;
    Button_Reply: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Button_ReplyClick(Sender: TObject);
    procedure Button_LoginClick(Sender: TObject);
    procedure Button_LogoutClick(Sender: TObject);
    procedure Button_UserClick(Sender: TObject);
    procedure FormStartDock(Sender: TObject; var DragObject: TDragDockObject);
  private
     giTid : Integer;
     giUid : Integer;
     //giUserId : Integer;
  public
        function GetUserIDByName(AName:String):Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
function TForm1.GetUserIDByName(AName: String): Integer;
begin
    ADOQuery.Close;
    ADOQuery.SQL.Text   := 'SELECT id FROM dw_user WHERE username='''+AName+'''';
    ADOQuery.Open;
    Result  := ADOQuery.Fields[0].AsInteger;
end;


procedure TForm1.Button_LoginClick(Sender: TObject);
begin
     dwOpenUrl(self,'/bbslogin','_self');

end;

procedure TForm1.Button_LogoutClick(Sender: TObject);
begin
    dwMessageDlg('确定要注销登录吗？','注销','确定','取消','query_logout',self);

end;

function _proc(AText:String):String;
begin
    Result  := StringReplace(AText,'''','''''',[rfReplaceAll]);
end;

procedure TForm1.Button_ReplyClick(Sender: TObject);
var
    sNow    : String;
    I       : Integer;
    iFid    : Integer;  //论坛id
    iTid    : Integer;
begin
    if Length(Trim(Edit_Subject.Text))<10 then begin
        dwMessage('主题不能少于10个字符!','error',self);
        Exit;
    end;
    if Length(Trim(Memo_New.Text))<10 then begin
        dwMessage('内容不能少于10个字符!','error',self);
        Exit;
    end;

    if Button_User.Visible then begin
        //先取得当前日期的PHP值备用
        sNow    := dwDateToPHPDate(Now).ToString;

        //取得论坛id
        iFid    := -1;
        for I := 0 to ComboBox_Rooms.GetCount-1 do begin
            if ComboBox_Rooms.Items[I] = ComboBox_Rooms.Text then begin
                iFid    := I;
                break;
            end;
        end;

        //更新bbs_thread
        ADOQuery.Close;
        ADOQuery.SQL.Text   := Format('INSERT INTO bbs_thread (fid,uid,lastuid,lastpid,subject,create_date,last_date,views,posts) '
                +'VALUES(%d,%d,%d,%d,''%s'',%s,%s,0,0)',[iFid+1,giUid,giUid,giUid,_proc(Edit_Subject.Text),sNow,sNow,0,0]);
        ADOQuery.ExecSQL;

        //取得tid
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'SELECT tid FROM bbs_thread'
                +' WHERE (uid='+giUid.ToString+') and (create_date='+sNow+')';
        ADOQuery.Open;
        iTid    := ADOQuery.Fields[0].AsInteger;

        //更新bbs_posts
        ADOQuery.Close;
        ADOQuery.SQL.Text    := Format('INSERT INTO bbs_post (tid,uid,create_date,message,message_fmt) '
                +'VALUES(%d,%d,%s,''%s'',''%s'')',[iTid,giUid,sNow,_proc(Memo_New.Text),_proc(Memo_New.Text)]);
        ADOQuery.ExecSQL;

        //更新dw_user
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'UPDATE dw_user SET last_date = '+sNow+', threads=threads+1 WHERE uid='+giUid.ToString;
        ADOQuery.ExecSQL;


        //
        dwOpenUrl(self,'/bbs','_self');

    end else begin
        dwOpenUrl(self,'/bbslogin','_self');
    end;
end;

procedure TForm1.Button_UserClick(Sender: TObject);
var
    iUserId : Integer;
begin
    //取得用户id
    iUserId := GetUserIdByName(Button_User.Caption);
    //
    dwShowMessage('欢迎欢迎！您是第 '+iUserId.ToString+' 位会员！',self);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    Top     := 0;
    //
    //giUserId    := -1;
end;

procedure TForm1.FormShow(Sender: TObject);
var
    sParams     : String;
    sUper       : string;
    sSubject    : string;
    sCookie     : string;

    //
    iCreate     : Integer;
    iPos        : Integer;
    iItem       : Integer;

    //
    oPanel      : TPanel;
    joUser      : Variant;

begin
    try
        //读cookie
        sCookie    := dwGetCookie(self,'bbs_user');
        sCookie    := dwAESDecrypt(sCookie,'bbs123');

        //如果读到cookie是json字符串,则设置用户信息
        if dwStrIsJson(sCookie) then begin
            //解决为json
            joUser     := _json(sCookie);
            //
            Button_User.Caption     := joUser.name;
            giUId                   := joUser.uid;
            //显隐控件
            Button_Logout.Left      := 9999;
            Button_User.Visible     := True;
            Button_Logout.Visible   := True;
            BUtton_Login.Visible    := False;
        end;

        //显示房间列表
        ADOQuery.Close;
        ADOQuery.SQL.Text   := 'SELECT [name] FROM bbs_forum ORDER BY fid';
        ADOQuery.Open;
        //
        ComboBox_Rooms.Items.Clear;
        while not ADOQuery.Eof do begin
            ComboBox_Rooms.Items.Add(ADOQuery.Fields[0].AsString);
            //
            ADOQuery.Next;
        end;
        //
        ComboBox_Rooms.ItemIndex    := 0;
    except
        dwOpenUrl(self,'/bbslogin','_self');
    end;

end;

procedure TForm1.FormStartDock(Sender: TObject; var DragObject: TDragDockObject);
var
    sMethod : string;
    sValue  : string;
begin
    sMethod := dwGetProp(Self,'interactionmethod');
    //通过类似以下得到返回的结果，为'1'时表示为"确定"，否则为"取消"
    sValue  := dwGetProp(Self,'interactionvalue');
    //
    if sMethod = 'query_logout' then  begin
        if sValue = '1' then begin
            //清除COOKIE
            dwSetCookie(self,'bbs_user','',0);
            //
            Button_Logout.Visible   := False;
            Button_User.Visible     := False;
            //
            Button_Login.Visible    := True;
            //
            //giUserID    := -1;
        end;
    end;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
     //
     if (X>600)and(Y>600) then begin
        Width     := X-24;
     end else begin
         Width  := 1110;
     end;
end;

end.
